package com.prs.crm.dao.hr.impl;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import com.prs.crm.dao.hr.CheckDao;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.domain.hr.Check;
import com.prs.crm.domain.hr.Person;



public class CheckDaoImpl extends BaseDaoImpl<Check> implements CheckDao{



	@Override
	public List<Check> getAllCheck() {
		return (List<Check>)this.getHibernateTemplate().execute(
				new HibernateCallback()
				{
					public Object doInHibernate(Session session) throws HibernateException {
						
				Criteria criteria = session.createCriteria(Check.class);
				return criteria.list();
					}
			  }
				
		);
	
	}

	@Override
	public void saveOrUpdate(Check check) {
		
		String  restTime=check.getRestTime();
		Date currentDate;
		currentDate = new Date();
		SimpleDateFormat df = new SimpleDateFormat("HH:mm");
		String currentDate1=df.format(currentDate);
		if(restTime==null)
		{
			check.setRestTime(currentDate1);
		}
			
		this.getHibernateTemplate().saveOrUpdate(check);
	}

	@Override
	@SuppressWarnings("unchecked")
	public Check getCheckByname(final String name, final String date) {
		
		return (Check) this.getHibernateTemplate().execute(new HibernateCallback(){

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(Check.class);
				criteria.add(Restrictions.eq("name", name));
				criteria.add(Restrictions.eq("date", date));
				if(name != null){
					   criteria.add(Restrictions.eq("name", name));
					 }				
					if(date!=null){
						criteria.add(Restrictions.eq("date",date));
						                                        
					}
					
				List<Check> result = (List<Check>) criteria
				.list();

		if (result.size() > 0)
		{
			   System.out.println(result.size());
				for(int i=0;i<result.size();i++){
						return result.get(i);
		
				}
		}
		     return null;
			}
		});
		
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Check> getChecks(final String name, final String date) {
		// TODO Auto-generated method stub
		return (List<Check>) this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(Check.class);
				criteria.add(Restrictions.eq("name", name));
				criteria.add(Restrictions.eq("date", date));
				return criteria.list();
			}
			
		});
	}

	public Collection<Check> getPersonChecksInMonth(final Integer userId,final String month)
	{
		return (Collection<Check>)this.getHibernateTemplate().execute(new HibernateCallback<Collection<Check>>() {
			@SuppressWarnings("unchecked")
			@Override
			public Collection<Check> doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				Criteria criteria = arg0.createCriteria(Check.class).createAlias("recorder", "r");
				 criteria.add(Restrictions.eq("r.id", userId));
				criteria.add(Restrictions.like("date",month+"%"));
				return (Collection<Check>)criteria.list();
				
			}
			
		});
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public List<Check> getCheckByPersonInMonth(final String name,final String months) {
		return (List<Check>)this.getHibernateTemplate().execute(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				Criteria criteria = arg0.createCriteria(Check.class);
				if(name != null){
				   criteria.add(Restrictions.eq("name", name));
				 }				
				if(months!=null){
					criteria.add(Restrictions.eq("date",months));
					                                        
				}

				return criteria.list();
			}
			
		});
	}

	
	
	@Override
	@SuppressWarnings("unchecked")
	public Collection<Object[]> getCheckByPersonydm(final Collection<Person> persons, final String date) {
		// TODO Auto-generated method stub
		return (Collection<Object[]>) this.getHibernateTemplate().execute(new HibernateCallback(){
			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub

				Criteria criteria = arg0.createCriteria(Check.class);
				if (persons!=null)
	        	{
	        		criteria.add(Restrictions.in("name", persons));
	        	}
				if (date!=null)
	        	{
				criteria.add(Restrictions.eq("date", date));
	        	}
				List<Check> result = (List<Check>) criteria.list();

		if (result.size() > 0)
			return result;
		else
			return null;
			}
			
		});
	}


}